define(['WebPDF', '../LayoutInfo', './ContinuousPageViewLayout', './SinglePageViewLayout'], 
		function (WebPDF, LayoutInfo, ContinuousPageViewLayout, SinglePageViewLayout) {
	WebPDF.factory('PageViewLayout', ["$log", function($log) {	
		'use strict';
 		function _adjustScale(scale) {
            //scale = parseInt((scale + 0.2) / 0.5) * 0.5;
            if (scale < LayoutInfo.minScale)
                return LayoutInfo.minScale;
            if (scale > LayoutInfo.maxScale)
                return LayoutInfo.maxScale;
            return scale;
        }

        function _calcFitWidthScale(maxPageWidth, docViewWidth) {
        	var marginX = LayoutInfo.marginX;        	       
            var innerWidth = docViewWidth - 2 * marginX;          
            var scale = innerWidth / (maxPageWidth * LayoutInfo.pixelsPerPoint);                 
            return scale;
        }

        function _calcFitPageScale(maxPageSize, docViewWidth, docViewHeight){
            var maxPageViewHeight = maxPageSize.height * LayoutInfo.pixelsPerPoint;
            var maxPageViewWidth = maxPageSize.width * LayoutInfo.pixelsPerPoint;
            return _getFitRectScale(maxPageViewWidth, maxPageViewHeight, docViewWidth, docViewHeight);
        }

        function _getFitRectScale(pageViewWidth, pageViewHeight, docViewWidth, docViewHeight) {           
           //[计算按横向大小来显示时的显示倍数] ;            
            var dbScale1 = docViewWidth / pageViewWidth;

            //[计算竖按向大小来显示时的显示倍数];           
            var dbScale2 = docViewHeight / pageViewHeight;
            //[拿一个最小的倍数来计算];
            return  Math.min(dbScale1, dbScale2);              
        }      

		function _calcScale(zoomMode, maxPageSize, docViewWidth, docViewHeight) {
			var newScale = parseInt(zoomMode);
			if (isNaN(newScale)) {
				switch (zoomMode) {					
					case 'FitWidth':
						newScale = _calcFitWidthScale(maxPageSize.width, docViewWidth);
						break;
					case 'FitPage':
						newScale = _calcFitPageScale(maxPageSize, docViewWidth, docViewHeight);
						break;
					case 'ActualSize':
						newScale = 1.0;
						break;		
					default:
						return 0;		
				}
			}
			else {
			 	newScale = newScale / 100;
			}			
            newScale = _adjustScale(newScale);    
            return newScale;
        }

        function _getMaxPageSize(pdfDoc) {    
        	var maxPageWidth = 0;
        	var maxPageHeight = 0;    
        	angular.forEach(pdfDoc.pdfPages, function(pdfPage, key) {        		
                maxPageWidth = Math.max(maxPageWidth, pdfPage.getPageWidth());     
                maxPageHeight = Math.max(maxPageHeight, pdfPage.getPageHeight()); 
        	});                  
            return {
            	"width" : maxPageWidth,
            	"height" : maxPageHeight
            };
        }
      
		function LayoutInstance(docView, pdfDoc, settings){			
			this.docView = docView;
			this.maxPageSize = _getMaxPageSize(pdfDoc);        
            this.pdfDoc = pdfDoc;     
           // this.scrollApi = docView.fwrJScrollPane(settings).data('jsp');   
            this.scrollApi = docView.WebPDFNativeScroll(settings).data('jsp'); 
            this.bindScroll();  
            var layout = new ContinuousPageViewLayout(docView, pdfDoc, settings);
            angular.extend(this, layout);     
		}

        var fn =  LayoutInstance.prototype;
		fn.getFitWidthScale = function () {     			    	            	
            return _calcFitWidthScale(this.maxPageSize.width, this.innerWidth());
        };

        fn.LayoutPageView = function (page, scale){    
    		var width = page.getPageWidth() * scale * LayoutInfo.pixelsPerPoint;
    		var height = page.getPageHeight() * scale * LayoutInfo.pixelsPerPoint;           
    		return {
    			"width" : parseInt(width),
    			"height" : parseInt(height),
    			'margin-left' : parseInt(Math.max(0, (this.innerWidth() - width) / 2)) 		
    		};
        };	

        fn.innerWidth = function() {           
            return this.docView.width() - this.scrollApi.getScrollBarWidth();
        };

        fn.innerHeight = function() {
            return this.docView.height() - this.scrollApi.getScrollBarHeight();
        };
        fn.calcScale = function(zoomMode) {
        	return _calcScale(zoomMode, this.maxPageSize, this.innerWidth(), this.innerHeight());
        };

        fn.calcScrollHeight = function(scale) {
            var pdfPages = this.pdfDoc.pdfPages;
            if (pdfPages.length === 0) return 0;
            var scrollHeight = (pdfPages[0].getPageHeight() * scale * LayoutInfo.pixelsPerPoint + LayoutInfo.marginY) * pdfPages.length;        
            return scrollHeight - LayoutInfo.marginY;
        };

        fn.setScale = function(scale){
            this.scale = scale;
            this.update();
        };

        fn.reSize = function(scale){           
            this.update();
        };

        fn.scroll = function(callback) {           
            (typeof callback == "function") ? this.docView.bind("scroll", callback) : this.docView.trigger("scroll", [callback]);
        };
        fn.update = function() {                
            //防止多次调用onScroll
            this.unBindScroll();           
            this.scrollApi.scrollToX(0);
            this.scrollApi.reinitialise();
            this.bindScroll();
            this.scroll();         
        };

        fn.gotoPage = function(pageIndex) {          
            var pageElem = this.scrollApi.getContentPane().children(':eq(' + pageIndex + ')');
            //防止多次调用onScroll
            this.unBindScroll();         
            this.scrollApi.scrollToElement(pageElem);
            this.bindScroll();
            this.scroll();   
        };

        fn.bindScroll = function() {
             this.docView.on("jsp-scroll-y", onScroll);
             var _self =  this;
             function onScroll(){
                _self.scroll();
             }
        };

        fn.unBindScroll = function() {
             this.docView.off("jsp-scroll-y");            
        };


        fn.getContentPane = function() {
            return this.scrollApi.getContentPane();
        };

        fn.createPDFPageView = function(callback) {
           $log.error("cannot not be called");
        };

        fn.movePage = function(offsetX, offsetY) {
            this.scrollApi.scrollBy(offsetX, offsetY);
        };
        return LayoutInstance;     	       	
	}]);
});